<?php
header('Content-Type: text/html; charset=utf-8');
include_once('./function.php');
include_once('./array.php');

//从网站获取图片
//保存到imgpath
set_time_limit(0);
$content = file_get_contents('http://www.czfdc.com.cn/esf/csindex.asp');
preg_match('#verifycode\.asp\?n=[\d\.]+#is',$content,$match);
if ( ! $match[0] ) exit('ERROR');
$img_url = 'http://www.czfdc.com.cn/KS_Inc/'. $match[0];
$imgpath = './'.time().'.bmp';
if(file_put_contents($imgpath, file_get_contents($img_url))===false){
	echo "<p>读取验证码失败.</p>";
}else{
	echo "<p>读取验证码...</p>";
}


$img = NewMagickWand();
MagickReadImage ( $img, $imgpath );
$h = MagickGetImageHeight($img);
$w = MagickGetImageWidth($img);
$pixel_wand=NewPixelWand();


echo "<p>分析验证码...";
$a = 'a.bmp';
cropImage($imgpath, 17, 20, 0, 0, $a);
echo " 1 ";
$b = 'b.bmp';
cropImage($imgpath, 17, 20, 17, 0, $b);
echo " 2 ";
$c = 'c.bmp';
cropImage($imgpath, 17, 20, 34, 0, $c);
echo " 3 ";
$d = 'd.bmp';
cropImage($imgpath, 17, 20, 51, 0, $d);
echo " 4 ";
$e = 'e.bmp';
cropImage($imgpath, 17, 20, 67, 0, $e);
echo " 5 ";
echo "</p>";

$savefilepath = time().'.bmp';
//$ok = MagickWriteImage($img,$savefilepath);
DestroyMagickWand($img);

//取1
$tmp = imagecreatefrombmp($a);
$arr1 = array();
foreach($tmp as $t){
	if($t == '255255255'){
		$arr1[] = 0;
	}else{
		$arr1[] = 1;
	}
}
$str1 = '';
foreach($arr1 as $x){
	$str1 .=$x;
}

//取2
$tmp = imagecreatefrombmp($b);
$arr1 = array();
foreach($tmp as $t){
	if($t == '255255255'){
		$arr1[] = 0;
	}else{
		$arr1[] = 1;
	}
}
$str2 = '';
foreach($arr1 as $x){
	$str2 .=$x;
}
//取3
$tmp = imagecreatefrombmp($c);
$arr1 = array();
foreach($tmp as $t){
	if($t == '255255255'){
		$arr1[] = 0;
	}else{
		$arr1[] = 1;
	}
}
$str3 = '';
foreach($arr1 as $x){
	$str3 .=$x;
}

//取4
$tmp = imagecreatefrombmp($d);
$arr1 = array();
foreach($tmp as $t){
	if($t == '255255255'){
		$arr1[] = 0;
	}else{
		$arr1[] = 1;
	}
}
$str4 = '';
foreach($arr1 as $x){
	$str4 .=$x;
}
//取5
$tmp = imagecreatefrombmp($e);
$arr1 = array();
foreach($tmp as $t){
	if($t == '255255255'){
		$arr1[] = 0;
	}else{
		$arr1[] = 1;
	}
}
$str5 = '';
foreach($arr1 as $x){
	$str5 .=$x;
}


foreach ($mks as $key => $value) {
	similar_text($value, $str1, $p);
    $maxArr[$key] = $p;           
}echo var_dump($maxArr);
$m1 = max($maxArr);
foreach($maxArr as $k => $v){
	if($v == $m1){
		$f1 = $k;
	}
}
foreach ($mks as $key => $value) {
	similar_text($value, $str2, $p);
    $maxArr[$key] = $p;           
}
$m1 = max($maxArr);
foreach($maxArr as $k => $v){
	if($v == $m1){
		$f2 = $k;
	}
}
foreach ($mks as $key => $value) {
	similar_text($value, $str3, $p);
    $maxArr[$key] = $p;           
}
$m1 = max($maxArr);
foreach($maxArr as $k => $v){
	if($v == $m1){
		$f3 = $k;
	}
}
foreach ($mks as $key => $value) {
	similar_text($value, $str4, $p);
    $maxArr[$key] = $p;           
}
$m1 = max($maxArr);
foreach($maxArr as $k => $v){
	if($v == $m1){
		$f4 = $k;
	}
}
foreach ($mks as $key => $value) {
	similar_text($value, $str5, $p);
    $maxArr[$key] = $p;           
}
$m1 = max($maxArr);
foreach($maxArr as $k => $v){
	if($v == $m1){
		$f5 = $k;
	}
}

echo $f1.$f2.$f3.$f4.$f5;


//$str1 = "<p>".$str1;
//$str1 = rtrim($str1, "<p>");
//echo $str1;
//echo count($str1);
//����ͼƬ�����Ϊһ�����ļ�
function cropImage($filepath, $width, $height, $x, $y, $destpath)
{
	$resource = NewMagickWand();
	MagickReadImage( $resource, $filepath);
	MagickCropImage( $resource, $width, $height, $x, $y); //bool MagickCropImage( MagickWand mgck_wnd, float width, float height, int x, int y )
	return MagickWriteImage($resource, $destpath);
}
?>